package com.jxs.android.cmcc.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jxs.android.cmcc.boot.SelfApplication;
import com.jxs.android.cmcc.data.MenuInfo;

/**
 * @Des 目录结构
 * @Author jiangxs
 * @Des 2012-8-31 上午9:16:50
 */
public class MenuHolder {

	static public interface Keys {
		public static final String MAIN_MENU_LEVEL = "1";
		public static final String SUB_MENU_LEVEL = "2";
		public static final String TABLE_MENU_LEVEL = "0";
	}

	static public interface Table {
		public static final String TABLE_NAME = "org_menu_tab";

		public static final String MENU_ID = "menu_id";
		public static final String MENU_NAME = "menu_name";
		public static final String SUPER_MENU = "super_menu";
		public static final String MENU_LVL = "menu_lvl";
		public static final String MENU_PIC_NAME = "menu_pic_name";
		public static final String MENU_POS = "menu_pos";
		public static final String MENU_STYLE = "menu_style";
		public static final String IS_NEW = "isnew";
		public static final String INTERFACE_ID = "interface_id";

		public static final String[] COLUMNS = new String[] { MENU_ID, MENU_NAME, SUPER_MENU, MENU_LVL, MENU_PIC_NAME, MENU_POS, MENU_STYLE, IS_NEW, INTERFACE_ID };
	}

	public static void creater(SQLiteDatabase db) {
		StringBuffer createSQL = new StringBuffer(" CREATE TABLE IF NOT EXISTS ");
		createSQL.append(Table.TABLE_NAME);
		createSQL.append(" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ");
		createSQL.append(Table.MENU_ID + " varchar(20),");
		createSQL.append(Table.MENU_NAME + " varchar(25),");
		createSQL.append(Table.SUPER_MENU + " varchar(20),");
		createSQL.append(Table.MENU_LVL + " varchar(20), ");
		createSQL.append(Table.MENU_PIC_NAME + " varchar(20), ");
		createSQL.append(Table.MENU_POS + " varchar(20), ");
		createSQL.append(Table.MENU_STYLE + " varchar(20), ");
		createSQL.append(Table.IS_NEW + " varchar(20), ");
		createSQL.append(Table.INTERFACE_ID + " varchar(20) ");
		createSQL.append(" )");
		db.execSQL(createSQL.toString());
	}

	public static void saveOrUpdate(MenuInfo menuInfo) {
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(MenuProvider.CONTENT_URI, new String[] { Table.MENU_NAME }, Table.MENU_ID + "=? ", new String[] { menuInfo.getMenu_id() }, null);
		ContentValues values = new ContentValues();

		values.put(Table.MENU_ID, menuInfo.getMenu_id());
		values.put(Table.MENU_NAME, menuInfo.getMenu_name());
		values.put(Table.SUPER_MENU, menuInfo.getSuper_menu());
		values.put(Table.MENU_LVL, menuInfo.getMenu_lvl());
		values.put(Table.MENU_PIC_NAME, menuInfo.getMenu_pic_name());
		values.put(Table.MENU_POS, menuInfo.getMenu_pos());
		values.put(Table.MENU_STYLE, menuInfo.getMenu_style());
		values.put(Table.IS_NEW, menuInfo.getIsnew());
		values.put(Table.INTERFACE_ID, menuInfo.getInterface_id());

		if (null != cursor && cursor.getCount() == 0) {
			contentResolver.insert(MenuProvider.CONTENT_URI, values);
		} else {
			contentResolver.update(MenuProvider.CONTENT_URI, values, Table.MENU_ID + "=? ", new String[] { menuInfo.getMenu_id() });
		}
		if (null != cursor) {
			cursor.close();
		}
	}

	public static HashMap<Integer, ArrayList<MenuInfo>> getMainMenuinfos() {
		HashMap<Integer, ArrayList<MenuInfo>> menuInfos = new HashMap<Integer, ArrayList<MenuInfo>>();
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(MenuProvider.CONTENT_URI, Table.COLUMNS, Table.MENU_LVL + "=? ", new String[] { Keys.MAIN_MENU_LEVEL }, Table.MENU_POS + " asc ");
		if (null != cursor && cursor.moveToFirst()) {
			while (true) {
				ArrayList<MenuInfo> results = new ArrayList<MenuInfo>();
				int index = 0;
				int page = 0;
				do {
					results.add(cuserMenuInfo(cursor));
					index++;
					menuInfos.put(page, results);
					if (index > 7) {
						index = 0;
						results = new ArrayList<MenuInfo>();
						page++;
					}
				} while (cursor.moveToNext());
				break;
			}
		}
		if (null != cursor) {
			cursor.close();
		}
		return menuInfos;
	}

	private static MenuInfo cuserMenuInfo(Cursor cursor) {
		MenuInfo menuInfo = new MenuInfo();
		menuInfo.setMenu_id(cursor.getString(cursor.getColumnIndex(Table.MENU_ID)));
		menuInfo.setMenu_name(cursor.getString(cursor.getColumnIndex(Table.MENU_NAME)));
		menuInfo.setSuper_menu(cursor.getString(cursor.getColumnIndex(Table.SUPER_MENU)));
		menuInfo.setMenu_lvl(cursor.getString(cursor.getColumnIndex(Table.MENU_LVL)));
		menuInfo.setMenu_pic_name(cursor.getString(cursor.getColumnIndex(Table.MENU_PIC_NAME)));
		menuInfo.setMenu_pos(cursor.getString(cursor.getColumnIndex(Table.MENU_POS)));
		menuInfo.setMenu_style(cursor.getString(cursor.getColumnIndex(Table.MENU_STYLE)));
		menuInfo.setIsnew(cursor.getString(cursor.getColumnIndex(Table.IS_NEW)));
		menuInfo.setInterface_id(cursor.getString(cursor.getColumnIndex(Table.INTERFACE_ID)));
		return menuInfo;
	}

	public static ArrayList<MenuInfo> getMenuInfos(String super_menu, String level) {
		ArrayList<MenuInfo> results = new ArrayList<MenuInfo>();
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(MenuProvider.CONTENT_URI, Table.COLUMNS, Table.SUPER_MENU + "=? and " + Table.MENU_LVL + " =? ", new String[] { super_menu, level }, Table.MENU_POS + " asc ");
		if (null != cursor && cursor.moveToFirst()) {
			do {
				results.add(cuserMenuInfo(cursor));
			} while (cursor.moveToNext());
		}
		if (null != cursor) {
			cursor.close();
		}
		return results;
	}

	public static MenuInfo getMenuInfo(String menu_id) {
		MenuInfo info = new MenuInfo();
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(MenuProvider.CONTENT_URI, Table.COLUMNS, Table.MENU_ID + "=? ", new String[] { menu_id }, null);
		if (null != cursor && cursor.moveToFirst()) {
			info = cuserMenuInfo(cursor);
		}
		if (null != cursor) {
			cursor.close();
		}
		return info;
	}

	public static int counter() {
		int counter = 0;
		Cursor cursor = SelfApplication.getApplication().getContentResolver().query(MenuProvider.CONTENT_URI, Table.COLUMNS, null, null, null);
		if (null != cursor) {
			counter = cursor.getCount();
			cursor.close();
		}
		return counter;
	}

}
